<template>
    <view class="pet-avatar-container">
        <!-- 消息气泡 -->
        <view class="message-bubble" v-if="showMessage">
            <view class="bubble-content">{{ message }}</view>
            <view class="bubble-arrow"></view>
        </view>

        <!-- 狐狸头像 -->
        <view class="fox-avatar" @tap="handleTap">
            <!-- 耳朵 -->
            <view class="ears">
                <view class="ear left" :class="{ 'ear-wiggle': isWiggling }"></view>
                <view class="ear right" :class="{ 'ear-wiggle': isWiggling }"></view>
            </view>

            <!-- 脸部 -->
            <view class="face">
                <!-- 眼睛 -->
                <view class="eyes">
                    <view class="eye left" :class="{ 'eye-blink': isBlinking }"></view>
                    <view class="eye right" :class="{ 'eye-blink': isBlinking }"></view>
                </view>

                <!-- 鼻子 -->
                <view class="nose"></view>

                <!-- 嘴巴 -->
                <view class="mouth" :class="{ 'mouth-talking': isTalking }"></view>
            </view>
        </view>
    </view>
</template>

<script>
export default {
    name: 'pet-avatar',
    props: {
        // 消息内容
        message: {
            type: String,
            default: ''
        },

        // 消息显示时间(毫秒)
        messageDuration: {
            type: Number,
            default: 5000
        },
        // 头像大小
        size: {
            type: [Number, String],
            default: '120rpx'
        }
    },
    data() {
        return {
            showMessage: false,
            isBlinking: true, // 是否眨眼
            isWiggling: true, // 是否摆动耳朵
            isTalking: false, // 是否说话
            blinkInterval: null, // 眨眼定时器
            wiggleInterval: null, // 耳朵摆动定时器
            talkingTimeout: null // 说话定时器
        };
    },

    mounted() {
        this.startBlinking();
        this.startRandomWiggle();
    },
    beforeDestroy() {
        this.clearAllAnimations();
    },
    methods: {
        // 处理点击事件
        handleTap() {
            this.$emit('click');
            this.wiggleEars();
        },

        // 开始眨眼动画
        startBlinking() {
            // 随机间隔眨眼
            this.blinkInterval = setInterval(() => {
                this.isBlinking = true;
                setTimeout(() => {
                    this.isBlinking = false;
                }, 200);
            }, 1000 + Math.random() * 2000);
        },

        // 开始随机摆动耳朵
        startRandomWiggle() {
            this.wiggleInterval = setInterval(() => {
                this.wiggleEars();
            }, 5000 + Math.random() * 5000);
        },

        // 摆动耳朵
        wiggleEars() {
            this.isWiggling = true;
            setTimeout(() => {
                this.isWiggling = false;
            }, 500);
        },

        // 开始说话动画
        startTalking() {
            this.isTalking = true;
            this.showMessage = true;
            this.wiggleEars();

            // 设置说话结束定时器
            if (this.talkingTimeout) {
                clearTimeout(this.talkingTimeout);
            }

            this.talkingTimeout = setTimeout(() => {
                this.stopTalking();
                this.$emit('message-end');
            }, this.messageDuration);
        },

        // 停止说话动画
        stopTalking() {
            this.isTalking = false;
            this.showMessage = false;
        },

        // 清除所有动画
        clearAllAnimations() {
            if (this.blinkInterval) clearInterval(this.blinkInterval);
            if (this.wiggleInterval) clearInterval(this.wiggleInterval);
            if (this.talkingTimeout) clearTimeout(this.talkingTimeout);
        }
    }
};
</script>

<style lang="scss" scoped>
.pet-avatar-container {
    position: fixed;
    right: 30rpx;
    bottom: calc(env(safe-area-inset-bottom) + 10rpx);
    z-index: 999;
    display: flex;
    flex-direction: column;
    align-items: center;
}

/* 消息气泡样式 */
.message-bubble {
    position: absolute;
    bottom: 160rpx;
    width: 60vw;
    right: 20rpx;
    background-color: #ffffff;
    border-radius: 20rpx;
    padding: 16rpx 24rpx;
    margin-bottom: 20rpx;
    box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
    max-width: 400rpx;
    word-break: break-word;
    animation: pop-in 0.3s ease-out;

    .bubble-content {
        font-size: 28rpx;
        color: #333;
    }

    .bubble-arrow {
        position: absolute;
        bottom: -16rpx;
        right: 30rpx;
        width: 0;
        height: 0;
        border-left: 16rpx solid transparent;
        border-right: 16rpx solid transparent;
        border-top: 16rpx solid #ffffff;
    }
}

/* 狐狸头像样式 */
.fox-avatar {
    width: 140rpx;
    height: 140rpx;
    position: relative;
    cursor: pointer;
    transition: transform 0.2s;

    &:active {
        transform: scale(0.95);
    }
}

/* 耳朵样式 */
.ears {
    position: absolute;
    width: 100%;
    height: 50%;
    top: -20%;
    display: flex;
    justify-content: space-between;

    .ear {
        width: 40%;
        height: 100%;
        background-color: #ff8c00;
        border-radius: 50% 50% 0 0;
        position: relative;
        transform-origin: bottom center;

        &:before {
            content: '';
            position: absolute;
            width: 60%;
            height: 60%;
            background-color: #ffa500;
            border-radius: 50% 50% 0 0;
            top: 10%;
            left: 20%;
        }

        &.left {
            transform: rotate(-10deg);
        }

        &.right {
            transform: rotate(10deg);
        }

        &.ear-wiggle {
            animation: ear-wiggle 0.5s ease;
        }
    }
}

/* 脸部样式 */
.face {
    width: 100%;
    height: 100%;
    background-color: #ffa500;
    border-radius: 50%;
    position: relative;
    overflow: hidden;
    box-shadow: 0 6rpx 16rpx rgba(0, 0, 0, 0.2);
}

/* 眼睛样式 */
.eyes {
    position: absolute;
    width: 80%;
    height: 30%;
    top: 25%;
    left: 10%;
    display: flex;
    justify-content: space-between;

    .eye {
        width: 30%;
        height: 100%;
        background-color: #ffffff;
        border-radius: 50%;
        position: relative;

        &:after {
            content: '';
            position: absolute;
            width: 40%;
            height: 40%;
            background-color: #000000;
            border-radius: 50%;
            top: 30%;
            left: 30%;
        }

        &.eye-blink {
            animation: eye-blink 0.2s ease;
        }
    }
}

/* 鼻子样式 */
.nose {
    position: absolute;
    width: 20%;
    height: 10%;
    background-color: #000000;
    border-radius: 50%;
    top: 55%;
    left: 40%;
}

/* 嘴巴样式 */
.mouth {
    position: absolute;
    width: 40%;
    height: 10%;
    background-color: #ff6347;
    border-radius: 20rpx;
    bottom: 17%;
    left: 30%;

    &.mouth-talking {
        animation: mouth-talking 0.3s infinite alternate;
    }
}

/* 动画定义 */
@keyframes eye-blink {
    0% {
        height: 100%;
    }
    50% {
        height: 10%;
    }
    100% {
        height: 100%;
    }
}

@keyframes ear-wiggle {
    0% {
        transform: rotate(-10deg);
    }
    25% {
        transform: rotate(-15deg);
    }
    50% {
        transform: rotate(-5deg);
    }
    75% {
        transform: rotate(-15deg);
    }
    100% {
        transform: rotate(-10deg);
    }
}

@keyframes mouth-talking {
    0% {
        height: 10%;
    }
    100% {
        height: 14%;
        border-radius: 50%;
    }
}

@keyframes pop-in {
    0% {
        transform: scale(0.5);
        opacity: 0;
    }
    100% {
        transform: scale(1);
        opacity: 1;
    }
}
</style>
